﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Csharp2Python3
{
    public partial class FormQsid : Form
    {
        public FormQsid()
        {
            InitializeComponent();
        }

        private void FormQsid_Load(object sender, EventArgs e)
        {
            var dbconfig = FileHelper.ReadFile("qsid2ip.json");

            Dictionary<string, Qsid> diction = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, Qsid>>(dbconfig);
      
            int row = 1;//行
            int column = 0;//列
            JObject obj = JObject.Parse(dbconfig);
            TableLayoutPanel layoiutpan =this. tableLayoutPanel1;
           
            int first_w = 30;
           


            foreach (string key in diction.Keys)
            {
                Qsid qsid = diction[key];

                TextBox box1 = new TextBox();
                box1.Text = key;
                box1.Width = first_w;
                box1.Name = key + "_key";
                box1.TextAlign = HorizontalAlignment.Left;

                TextBox box2 = new TextBox();
                box2.Text = qsid.name;
                box2.Name = key + "_name";
                box2.TextAlign = HorizontalAlignment.Left;

                TextBox box3 = new TextBox();
                box3.Text = qsid.ip;
                box3.Name = key + "_ip";
                box3.TextAlign = HorizontalAlignment.Left;

                TextBox box4 = new TextBox();
                box4.Text = qsid.port.ToString();
                box4.Name = key+"_port";
                box4.TextAlign = HorizontalAlignment.Left;

                layoiutpan.Controls.Add(box1, column, row);
                column++;
                layoiutpan.Controls.Add(box2, column, row);
                column++;
                layoiutpan.Controls.Add(box3, column, row);
                column++;
                layoiutpan.Controls.Add(box4, column, row);
                column = 0;
                row++;
                layoiutpan.RowCount++;
            }
            layoiutpan.Height = row * 30;
            TextBox box11 = new TextBox();
            box11.Text = "";
            box11.Width = first_w;
            box11.Name = "" + "_key";
            box11.TextAlign = HorizontalAlignment.Left;

            TextBox box22 = new TextBox();
            box22.Text = "";
            box22.Name = "" + "_name";
            box22.TextAlign = HorizontalAlignment.Left;

            TextBox box33 = new TextBox();
            box33.Text = "";
            box33.Name = "" + "_ip";
            box33.TextAlign = HorizontalAlignment.Left;

            TextBox box44 = new TextBox();
            box44.Text = "";
            box44.Name = "" + "_port";
            box44.TextAlign = HorizontalAlignment.Left;

            layoiutpan.Controls.Add(box11, 0, row);
            //column++;
            layoiutpan.Controls.Add(box22, 1, row);
            // column++;
            layoiutpan.Controls.Add(box33, 2, row);
            // column++;
            layoiutpan.Controls.Add(box44, 3, row);
            //Button btn1 = new Button();
            //btn1.Text = "保存";
            //layoiutpan.Controls.Add(btn1, 3, row);
            //btn1.MouseClick += new MouseEventHandler(button_MouseClick);


        }

        private void button1_Click(object sender, EventArgs e)
        {
            Dictionary<string, Qsid> root = new Dictionary<string, Qsid>();

            Qsid clild = new Qsid();
            Qsid lastclild = new Qsid();
            int index = 1;
            string key = "";
            foreach (Control c in tableLayoutPanel1.Controls)
            {
                //.// t
                if (c is TextBox)
                {
                    TextBox box = (TextBox)c;
                    Console.WriteLine(box.Name + "-->" + box.Text);
                    // Console.WriteLine(box.Name, box.Text);
                    if (index == 1)
                    {
                        clild = new Qsid();
                       
                    }
                    if (box.Name.StartsWith("_"))
                    {
                        if (box.Text.Length == 0)
                        {
                            continue;
                        }
                        switch (index)
                        {
                           case 1:
                                //clild.name = "";
                                key =box.Text;
                                break;
                            case 2:
                                clild.name = box.Text;
                                break;
                            case 3:
                                clild.ip = box.Text; 
                                break;
                            case 4:
                                clild.port = Int32.Parse(box.Text);
                                index = 0;
                                
                                root.Add(key, clild);
                                key = "";
                                break;
                             
                         
                        }
                

                    }
                    else
                    {

                      string[] names=box.Name.Split('_');
                      string  name = names[1];


                        switch (name)
                        {
                            case "key":
                                key= box.Text;
                                break;
                            case "name":
                                clild.name = box.Text;
                                break; 
                            case "ip":
                                clild.ip = box.Text;
                                break;
                            case "port":
                                clild.port = Int32.Parse(box.Text);
                                break;
                            default:
                                break;
                        }
                        if (index == 4)
                        {
                            index = 0;
                           
                            root.Add(key, clild);
                            key = "";
                        }
                       


                    }
                    index++;

                }

            }
            Console.WriteLine(JsonConvert.SerializeObject(root));
            FileHelper.WriteFile("qsid2ip.json", JsonConvert.SerializeObject(root));
            MessageBox.Show("保存成功");



        }
    }
}
